package org.innovasix.educational.javap.domain.constantpool;

import org.innovasix.educational.javap.domain.datatypes.cp_info;
import org.innovasix.educational.javap.domain.datatypes.u1;
import org.innovasix.educational.javap.domain.datatypes.u2;
import org.innovasix.educational.javap.domain.parser.ByteQueue;

/**
 * <p>
 * <b><i>From the JVM specification:</i></b><br/>
 * The CONSTANT_NameAndType_info structure is used to represent a field or method, without indicating which class or 
 * interface type it belongs to.
 * </p>
 * 
 * @see <a
 *      href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.1">4.4.1.
 *      The CONSTANT_Class_info Structure</a>
 * 
 * @author sven.mentl@gmail.com
 * 
 */
public class CONSTANT_NameAndType_info extends cp_info {

	public CONSTANT_NameAndType_info(ByteQueue byteQueue) {
		name_index = byteQueue.pollTwoBytes(CONSTANT_NameAndType_info.class.getCanonicalName());
		descriptor_index = byteQueue.pollTwoBytes(CONSTANT_NameAndType_info.class.getCanonicalName());
	}

	/**
	 * <p>
	 * <b><i>From the JVM specification:</i></b><br/>
	 * The tag item has the value CONSTANT_NameAndType(12).
	 * </p>
	 */
	final u1 tag = new u1(ConstantPoolTags.CONSTANT_NameAndType.decimalValue());

	/**
	 * <p>
	 * <b><i>From the JVM specification:</i></b><br/>
	 * The value of the name_index item must be a valid index into the
	 * constant_pool table. The constant_pool entry at that index must be a
	 * CONSTANT_Utf8_info structure representing either the special method name <init> 
	 * or a valid unqualified name denoting a field or method.
	 */
	u2 name_index;
	
	/**
	 * <p>
	 * <b><i>From the JVM specification:</i></b><br/>
	 * The value of the descriptor_index item must be a valid index into the
	 * constant_pool table. The constant_pool entry at that index must be a
	 * CONSTANT_Utf8_info structure representing a valid field descriptor or method descriptor.
	 */
	u2 descriptor_index;
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("CONSTANT_NameAndType_info [");
		sb.append("name_index: #");
		sb.append(name_index);
		sb.append(" descriptor_index: #");
		sb.append(descriptor_index);
		sb.append("]");
		return sb.toString();
	}

}
